package com.liuyanzhao.ssm.blog.controller.admin;

import com.liuyanzhao.ssm.blog.entity.App;
import com.liuyanzhao.ssm.blog.service.AppService;
import com.liuyanzhao.ssm.blog.util.ExportExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.apache.poi.hssf.usermodel.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(("Excel"))
public class ExcelController {
    @Autowired
    private AppService appService;
//    @RequestMapping(value = "/app")
//    public String exportExcel()  {
//        List<App> staffs = new ArrayList<App>();
//        List<App> apps = appService.listApp(null);
//        Map<String,String> titleMap = new LinkedHashMap<String,String>();
//        int i=1;
//        for (App app : apps) {
//            app.setAppId(i++);
//            staffs.add(app);
//        }
//        titleMap.put("appId", "应用号");
//        titleMap.put("appName", "应用名称");
//        titleMap.put("csName", "参数名称");
//        titleMap.put("csValue", "参数取值");
//        titleMap.put("csBeizhu", "参数使用说明");
//        titleMap.put("csStatus", "参数状态");
//        titleMap.put("createTime", "创建时间");
//        titleMap.put("updateTime", "修改时间");
//        String sheetName = "应用参数导出";
//        System.out.println("start导出");
//        ExportExcel.excelExport(staffs, titleMap, sheetName);
//        System.out.println("end导出");
//        return "redirect:/admin/app";
//    }
    @RequestMapping(value = "/app")
    public void Export(HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");
        List<App> classmateList = appService.listApp(null);

        // 设置要导出的文件的名字
        String fileName = "apps"  + new Date() + ".xls";

        // 新增数据行，并且设置单元格数据
        int rowNum = 1;

        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = { "Id", "appName", "csName", "csValue","csBeizhu","csStatus","createTime","updateTime"};
        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        int i=1;
        for (App app : classmateList) {
            app.setAppId(i++);
        }

        //在表中存放查询到的数据放入对应的列
        for (App item : classmateList) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(item.getAppId());
            row1.createCell(1).setCellValue(item.getAppName());
            row1.createCell(2).setCellValue(item.getCsName());
            row1.createCell(3).setCellValue(item.getCsValue());
            row1.createCell(4).setCellValue(item.getCsBeizhu());
            row1.createCell(5).setCellValue(item.getCsStatus());
            row1.createCell(6).setCellValue(item.getCreateTime());
            row1.createCell(7).setCellValue(item.getUpdateTime());
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }
}
